'''
Author: Liu Zhi Biao
Date: 2023-02-15 16:45:55
LastEditors: Liu Zhi Biao
LastEditTime: 2023-02-17 09:33:19
FilePath: \Runoob_basic\runoob2.py
Description: 

Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
'''

from random import randint
import sys

class runoob:
    def __del__(self):
        pass

    def fbnq(self):
        a = 0
        b = 1
        while a < 10:
            print(a, end = ' ')
            c = b
            b = a + b
            a = c

    def fbnq2(slef):
        a, b = 0, 1
        while a < 10:
            print(a, end = ' ')
            a , b = b, a + b    # 先计算，后赋值

    def myif(self):
        age = int(input("请输入你的年纪"))
        print("")
        if age <= 0:
            print("不可能")
        elif age == 1:
            print("一岁")
        elif age >= 2 and age <= 16:
            print('少年')
        elif age >= 16 and age <= 30:
            print('青年')
        elif age >= 30 and age <= 50:
            print('中年')
        else:
            print('老年')

    def guess_number(self):
        number = randint(0, 1000)
        guess = -1
        while guess != number:
            guess = int(input("请输入数字："))
            if guess == number:
                print("恭喜你答对了, 数字是：", number)
            elif guess > number:
                print("猜的数字大了")
            elif guess < number:
                print("猜的数字小了")

    # python 10 才有的新特性
    # def mymatch(self):
    #     statue = 1
    #     match statue:
    #         case 1:
    #             print("1")
    #         case 2:
    #             print("2")
    #         case _:
    #             print("none")

    # while 循环的 else 有点新意
    def mysum(self):
        n = 1000
        sum = 0
        count = 1
        while count <= n:
            sum += count
            count += 1
            print(f"{count} 小于 {n}")
        else:
            print(f"sum from 1 to {n} is {sum}")

    def myfor(self):
        # python for 循环可以遍历任何可迭代对象 如一个列表或者一个字符串
        names = ["xiaoming", "xiaohong", "xiaogang", "dabidou"]
        for name in names:
            print(name)

        for cell in names[0]:
            print(cell)

        # 若中途遇到break语句，则不执行
        # 若中途遇到continue语句，则执行下一循环，爬虫中用的很多
        for number in range(1, 9):
            print(number)
        else:
            print("end of names")

    # 迭代器操作
    def myiter(self):
        # 迭代器是一个可以记住遍历位置的对象
        # 从第一个元素开始，只能前进不能后退
        # iter() next()
        list = [1, 2, 3, 4, 5]
        it = iter(list)
        # print(next(it))
        for temp in it:
            print(temp, end=' ')

    def myiter2(list):
        list = [1, 2, 3, 4, 5]
        it = iter(list)
        while True:
            try:
                print(next(it))
            except StopIteration:
                sys.exit()

    # 迭代器高级操作
    def __iter__(self):
        self.a = 1
        return self
    
    def __next__(self):
        if self.a <= 5:
            x = self.a
            self.a += 1
            return x
        else:
            raise StopIteration
        
    # 生成器
    def fibonacci(self, n):
        a, b, counter = 0, 1, 0
        while True:
            if (counter < n):
                return
            yield a
            a, b = b, a + b
            counter += 1

    def fibonacci_test(self):
        f = self.fibonacci(10)
        while True:
            try:
                print(next(f), end="")
            except StopIteration:
                sys.exit()



    def __init__(self):
        # self.fbnq()
        # self.fbnq2()
        # self.myif()
        # self.guess_number()
        # self.mymatch()
        # self.mysum()
        # self.myfor()
        # self.myiter()
        # self.myiter2()
        self.fibonacci_test()
        pass

if __name__ == "__main__":
    run = runoob()

    # myiter = iter(run)
    # print(next(myiter))
    # print(next(myiter))

    for i in iter(run):
        print(i)

    
